package niuke;
import java.util.*;
class Drink{
    private int amount; //数量
    private double price; //单价
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    Drink(int amount,double price){
        this.amount=amount;
        this.price=price;
    }
    public double getFee() {
        return 0.0;
    }
}
class Tea extends Drink{
    public Tea(int amount, double price) {
        super(amount, price);
    }
    public double getFee(){
        return 1.5*this.getAmount()*this.getPrice();
    }
}
class Coffee extends Drink{
    public Coffee(int amount,double price){
        super(amount,price);
    }
    public double getFee(){
        if (this.getAmount()<4){
            return 2.0*this.getAmount()*this.getPrice();
        }
        return 1.5*this.getPrice()*this.getAmount();
    }
}


//自定义数据异常类，构造异常消息

class DataException extends Exception{

    private String message;//异常消息

    public DataException(int m,int n) {
        message=m+"是负数或"+n+"正数,不符合需求";
    }
    public String warnMess() {
        return message;
    }
}

//定义业务类：银行类，要求入账用正数表示，支出资金用负数表示。

//设计income(int in,int out)方法计算银行的收益，计算时会检查数据有效性，无效数据会抛出数据异常。

//in 表示入账资金，out表示支出资金

class Bank{

    private int money;//银行资金

    //按照数据需求，对数据有效性检查，无效时抛出自定义数据异常对象
    public void income(int in ,int out) throws DataException{
        if(in<0||out>0) {
            throw new DataException(in, out);
        }

        int netIncome=in+out;
        System.out.println("纯收入："+netIncome);

        money=money+netIncome;//银行资金更新
    }

    public int getMoney() {
        return money;
    }

}
class Fruit{
    public Fruit(int num)
    {//父类方法的定义
        System.out.println("现在有"+num+"个水果");
    }
}
class Apple extends Fruit{
    public Apple(int num){
        super(num);
    }
    //保证正确重写方法的注解
    public void showNumber(int num) {
        System.out.println("现在有"+num+"个苹果");
    }
}
class People{
    private String name;
    private int age;
    People(String name,int age){ this.name=name; this.age=age;    }
    public String toString() {
        return "[姓名："+name+",年龄："+age+"]";
    }
}
class SumList extends Thread{//声明线程类
    private ArrayList<Integer> lt=null;//整数序列
    private int sum; //序列和
    SumList(ArrayList<Integer> lt){ this.lt=lt; }
    //返回求和结果值
    public int getSum() { return sum; }
    //线程体
    public void run() {//线程类必须重写 run() 方法
        sum=0;
        for (int i = 0; i < lt.size(); i++) sum=sum+lt.get(i);
    }
}
public class niuKeMain10 {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int m=sc.nextInt();//输入序列中的整数个数
        ArrayList<Integer> lt=new ArrayList<Integer>();
        for (int j = 0; j < m; j++) lt.add(sc.nextInt());//输入序列
        SumList sum=new SumList(lt);//创建线程对象


        sum.run(); //启动线程


        sum.join(); //等待线程结束
        System.out.println(sum.getSum());//输出求和结果
    }
    public static void main3(String[] args) {
        People p=new People("huang",25);
        Class cls= p.getClass(); //获取对象 p 所属的类
        System.out.println( cls.getName());///获取该类的名字
    }
    public static void main1(String args[]) {
        Bank bank = new Bank();

        int in=0;//入账资金
        int out=0;//支出资金

        //输入入账资金和支出资金
        Scanner sc = new Scanner(System.in);
        in=sc.nextInt();
        out=sc.nextInt();

        try {
            bank.income(in,out);
        }
        catch(DataException e) {
            System.out.println("计算收益的过程出现如下问题：");
            System.out.println(e.warnMess());
        }

        System.out.println("银行目前有"+bank.getMoney()+"元");
    }
    public static void main2(String args[]) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student("2016001","rose",18));
        list.add(new Student("2016002","hunifu",19));
        list.add(new Student("2016003","britsh",20));
        Iterator<Student> it = (Iterator<Student>) list; //创建迭代器
        while (it.hasNext()) { //判断遍历是否结束
            Student stu= it.next();//取列表元素
            System.out.println(stu);
        }
    }
}

